<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@include file="/common/taglibs.jsp"%>
<script type="text/javascript">
$(function() {
    parent.$.messager.progress('close');

    $('#form').form({
        url : contextPath+"/param/editParam.action",
        onSubmit : function() {
            parent.$.messager.progress({
                title : '提示',
                text : '数据处理中，请稍后....'
            });
            //检查Form参数合法性,返回false不提交
           if(!checkForm()){
               parent.$.messager.progress('close');
                return false;
            }
        },
        success : function(data) {
            parent.$.messager.progress('close');
            data = JSON.parse(data);
            if(data && "success" == data.result){
           	 parent.$.messager.alert('提示信息', data.msg, 'info');
           	 parent.$.modalDialog.handler.dialog('close');
          	     parent.$.modalDialog.openner_dataGrid.datagrid('reload');//之所以能在这里调用到parent.$.modalDialog.openner_dataGrid这个对象，是因为param.jsp页面预定义好了
            } else {
                parent.$.messager.alert('错误', data.msg, 'error');
            }
        }
    });
});

function checkForm(){
   var isValid = true;
   var checkParamUrl = contextPath + "/param/checkParam.action";
   
   var nameEn = $("#form").find("[name='nameEn']").validatebox({
       required: true,
   	   validType:['length[1,100]','remoteValid[\''+checkParamUrl+'\',\'nameEn\',\'输入的系统参数已存在\',\'${paramk.nameEn}\']']
   });
   isValid = isValid && nameEn.validatebox('isValid');
   
   var nameZn = $("#form").find("[name='nameZn']").validatebox({
       required: true,
       validType:['length[1,100]']
   });
   isValid = isValid && nameZn.validatebox('isValid');
   
   var description = $("#form").find("[name='description']").validatebox({
       validType:['length[0,250]']
   });
   isValid = isValid && description.validatebox('isValid');
   
   //检查细表
   var paramKey = $("#form").find("[name='paramKey']").validatebox({
       required: true,
       validType:['length[1,100]']
   });
   var paramValues = $("#form").find("[name='paramValues']").validatebox({
       required: true,
       validType:['length[1,100]']
   });
   
   
   if(paramKey.length == 0){//针对细表记录为空情况
	   return isValid;
   }else{
	   var paramKeyVal = "";
	   for(var i=0;i<paramKey.length;i++){//逐行检查空情况
		   paramKeyVal = $.trim(paramKey[i].value);
		   if(isEmpty(paramKeyVal)){
			   isValid = false;
		   }
		   
		 //检查细项key是否有重复
	 		for(var j=0;j<paramKey.length;j++){
	 			if(($.trim(paramKey[i].value) != "") && ($.trim(paramKey[j].value) != "")){
	 				if(($.trim(paramKey[i].value) == $.trim(paramKey[j].value)) && (i != j)){
	 	 				$.messager.alert('提示信息', '参数key不能有重复', 'info');
	 	 				isValid = false;
	 	 				return isValid;
	 	 			} 
	 			}
	 		}
	   }
   }
   if(paramValues.length == 0){//针对细表记录为空情况
 	   return isValid;
   }else{
	   var paramValuesVal = "";
	   for(var i=0;i<paramValues.length;i++){//逐行检查空情况
		   paramValuesVal = $.trim(paramValues[i].value);
		   if(isEmpty(paramValuesVal)){
			   isValid = false;
		   }
	   }
   }
   
   return isValid;
}
</script>
<!-- edit-dialog begin -->
<div class="edit-param-context">
   <div class="p5">
        <form id="form" method="post">
           <table class="tablewidth1">
        		<tr>
        			<td align="right">
        				标识：
        			</td>
        			<td>
        				<input id="paramId" name="id" type="hidden" value="${paramk.id}"/>
        				<input id="nameEn" name="nameEn" class="span2" type="text" value="${paramk.nameEn}" style="width: 165px;"/>
        			</td>
        			<td align="right">
        				名称：
        			</td>
        			<td>
        				<input id="nameZn" name="nameZn" class="span2"  type="text" value="${paramk.nameZn}" style="width: 165px;"/>
        			</td>
        		</tr>
        		<tr>
        			<td align="right">
        				描述信息：
        			</td>
        			<td colspan="3">
        				<textarea id="description" name="description" class="" style="height: 80px;overflow: auto;line-height: 18px;width: 455px;">${paramk.description}</textarea>
        			</td>
        		</tr>
        	</table>
           <div class="fn-clear"></div>
           
           <!-- 明细 -->
           <div>
                	<font style="font-weight: bold;">明细：</font>
                	<a id="addDetail" class="easyui-linkbutton" iconCls="icon-add" plain="false" onclick="javascript:addRow();">添加</a>
                </div>
                <div class="ddtl1" align="center">
                	<table class="tabledtl1" id="paramValueTabId">
                		<tr>
                			<td width="10%" align="center">编号</td>
                			<td width="40%">参数key值</td>
                			<td width="40%">参数value值</td>
                			<td width="10%">操作</td>
                		</tr>
               			<c:forEach items="${paramk.paramList }" var="par" varStatus="status">
			                <tr>
	                			<td align="center">
		                			<span>${status.count}</span>
		                			<input id="paramValueIds${status.count}" name="paramValueIds" type="hidden" value="${par.id }"/>
	                			</td>
	                			<td><input id="paramKey${status.count}" name="paramKey" class="span2" type="text" style="width: 180px;" value="${par.paramKey }"/></td>
	                			<td><input id="paramValues${status.count}" name="paramValues" class="span2" type="text" style="width: 180px;" value="${par.paramValue }"/></td>
	                			<td><input id="delBtn${status.count}" name="delBtn" type="button" value="删除" onclick="javascript:delRow(this.parentElement.parentElement);"/></td>
                			</tr>
			            </c:forEach>
                	</table>
                </div>
       </form>
   </div>
</div> 
<!-- edit-dialog end -->

<script type="text/javascript">
    var rowCount = '${fn:length(paramk.paramList) + 1}';
    function addRow(){
		$("#paramValueTabId").append("<tr>"+
		"<td align=\"center\">"+rowCount+
		"<input id='paramValueIds"+rowCount+"' name='paramValueIds' type='hidden' value=''/></td>"+
		"<td><input id='paramKey"+rowCount+"' name='paramKey' class='span2' type='text' style=\"width: 180px;\"/></td>"+
		"<td><input id='paramValues"+rowCount+"' name='paramValues' class='span2' type='text' style=\"width: 180px;\"/></td>"+
    	"<td><input id='delBtn"+rowCount+"' name='delBtn' type='button' value='删除' onclick='javascript:delRow(this.parentElement.parentElement)'/></td>"+
    	"</tr>");
		
		rowCount++;
	}
	
	function delRow(rowObj){
		rowObj.parentElement.deleteRow(rowObj.rowIndex);
		caculateFob();
	}			
</script>